from datetime import datetime, timezone
from sqlalchemy import ForeignKey, String
from sqlalchemy.orm import Mapped, mapped_column, relationship
from typing import TYPE_CHECKING

from .base import Base


if TYPE_CHECKING:
    from .user import User
    from .ads import ADS


class SearchOrder(Base):
    __tablename__ = "search_order"

    id: Mapped[int] = mapped_column(primary_key=True)
    rubric_url: Mapped[str] = mapped_column(String(250), nullable=True)
    query_string: Mapped[str] = mapped_column(String(320), nullable=False)
    deep_pages: Mapped[int] = mapped_column(default=1)
    city: Mapped[str] = mapped_column(nullable=True)
    main_url: Mapped[str] = mapped_column(String(32), nullable=True)
    created_at: Mapped[datetime] = mapped_column(default=datetime.now(timezone.utc))
    user_id: Mapped[int] = mapped_column(ForeignKey("user.id", ondelete="CASCADE"))

    user: Mapped["User"] = relationship()
    ads: Mapped[list["ADS"]] = relationship(back_populates="order")
